<?php
$sid = (isset($_GET["sid"]))?$_GET["sid"]:-1;
$sids = $_SESSION["ArraySec"];
if($_SESSION['Permit']==1){
	$sql = "select id,name from tblsection";
}else{
	$sql = "select id,name from tblsection where id in ($sids)";
}
$sections = mysql_query($sql);
$tb="tblarticle";
$start=(isset($_GET["st"]))?$_GET["st"]:0;
$limit=100;
function GetPageLinks($Sql,$PageSize)
{
    
	$result=mysql_query ($Sql);
	if (!$result or mysql_num_rows ($result)==0)
	{			
	}
	else
	{
		$line=mysql_fetch_array($result);
        
		$Pages=ceil(mysql_num_rows ($result)/$PageSize);        
		if ($Pages>1)
		{
			$PageLink="Trang ";
			for ($i=0;$i<=$Pages-1;$i++)
			{
				$j=$i+1;
				if ($j==$_GET["page"])
					{
						$PageLink.=" {$j} | ";
					}
				else
					{
						$NewGet="";
						reset ($_GET);
						while (list($key, $val) = each($_GET))
						 {						   
						   if ($key!='page')						   
						   {
								$NewGet.="&{$key}={$val}";
						   }
						   
						}
						$NewGet.="&page={$j}";
						$NewGet=substr($NewGet,1);
						
						$PageLink.=" <a href='?{$NewGet}'>{$j}</a> | ";
					} 
			}
			$PageLink=substr($PageLink,0,-2);
		}
	}
	return $PageLink;
}
/*
function LoadList($Sql,$PageSize)
{
	
// Cau lenh truy van chinh khi chua phan trang:
// Tinh toan so luong ban ghi tra ve:		
		$PageLinks= GetPageLinks($Sql,$PageSize);
		if (isset ($_GET["page"]) and is_numeric ($_GET["page"]))
		{
			$StartNum=$PageSize * ($_GET["page"]-1); // Xac dinh vi tri ban ghi bat dau		
		}
		else
		{
			$StartNum=0;
		}
//Tiep tuc xay dung cau lenh truy van de lay du lieu theo trang
$result=mysql_query ($Sql); 
while ($row = mysql_fetch_array($result))
               {
                    if ($row[2]==date("m")){
			         $tongthang =$tongthang + $row[5];
}}

		$Sql.= " Limit {$StartNum}, {$PageSize}";		
		$result=mysql_query ($Sql);
		if (!$result or mysql_num_rows ($result)==0)
			{
				$tmp="Không có dữ liệu";
			}
		else
			{				
				//========== Xuất dữ liệu tại đây
                $stt=0;
               while ($row = mysql_fetch_array($result))
               {
                    if ($row[2]==date("m")){
			       
                     $stt=$stt+1;          
                     echo "<tr><td>{$stt}</td><td>$row[1]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td><a href='?pro={$_GET["pro"]}&id=$row[0]'>Xóa</a></td></tr>";
                     }
                } 
               
				$tmp="<tr align='right'><td colspan='7'>$PageLinks</td></tr> ";
                $tmp.="<tr align='right'><td colspan='7'>Tháng ".date("m")."</font> Đã chi hết:$tongthang</td></tr> ";
                echo $tmp;
                
			}
	
	//	return $tmp;
}
*/
if($sid ==-1){
	if($_SESSION['Permit']==1)
 		$sql = "Select $tb.id,$tb.created_date,$tb.image,$tb.tag,$tb.vi_title,$tb.en_title,$tb.published,tbluser.username as username,tblsection.name as sname FROM $tb Inner join tblsection on $tb.section_id  = tblsection.id Inner join tbluser on $tb.user_id=tbluser.id ";
	else
		$sql = "Select $tb.id,$tb.created_date,$tb.image,$tb.tag,$tb.vi_title,$tb.en_title,$tb.published,tbluser.username as username,tblsection.name as sname FROM $tb Inner join tblsection on $tb.section_id  = tblsection.id Inner join tbluser on $tb.user_id=tbluser.id WHERE $tb.section_id in ($sids)";
}
else
{
	$sql = "Select $tb.id,$tb.created_date,$tb.image,$tb.tag,$tb.vi_title,$tb.en_title,$tb.published,tbluser.username as username,tblsection.name as sname FROM $tb Inner join tblsection on $tb.section_id  = tblsection.id Inner join tbluser on $tb.user_id=tbluser.id WHERE $tb.section_id = $sid";
    }
$PageSize = 10;
$CountSQL= mysql_num_rows(mysql_query($sql));
$PageLinks= GetPageLinks($sql,$PageSize);
		if (isset ($_GET["page"]) and is_numeric ($_GET["page"]))
		{
			$StartNum=$PageSize * ($_GET["page"]-1); // Xac dinh vi tri ban ghi bat dau		
		}
		else
		{
			$StartNum=0;
		}
	$sql.= " Limit {$StartNum}, {$PageSize}";
//echo $sql.$PageLinks;
$rs=mysql_query($sql);
?>
<script language="javascript" type="text/javascript">
function delArticle(aid){
	if(confirm("Bạn có chắc chắn muốn xóa bài viết này?")){
		location="delarticle?aid="+aid;
	}
}
function slcSection_change(){
	var sid = document.getElementById("slcSection").value;
	var url = location.href;
	if(url.indexOf("?")!=-1){
		
		url= url.substring(0,url.indexOf("?"));
	}
	if(sid!=0){
		url+="?sid="+sid;
	}
	location.href= url;
}
</script>
<div align="center" style="margin:0px auto">
  <table cellpadding="3" width="740" border="0" align="center">
  
  <tr>
    <td colspan="8"><h3>Danh sách bài viết</h3></td>
  </tr>
  <tr>
    <td height="37" colspan="7">
    	<label>Chuyên mục</label>
    	<select id="slcSection" onchange="slcSection_change();">
    		<option value="0">Tất cả các chuyên mục</option>
    	<?php
    	 while($section=mysql_fetch_object($sections)){
    	 	if($section->id == $sid){
    	 		echo "<option selected = 'selected' value='$section->id'>$section->name</option>";
    	 	}else{
    	 		echo "<option value='$section->id'>$section->name</option>";
    	 	}
    	 }
    	?>
    	</select>
	</td>
    <td align="right">
    	<a  href="addarticle" title="Tạo bài viết">
    		<img src="img/Add.png" style="border:none;" width="32" height="32"/>
    	</a>
    </td>
  </tr>
  <tr bgcolor="#008000" style="color:white; height:30px">
    <th align="center" width="5%">ID</th>
    <th width="20%"><strong>Tiêu đề Tiếng Việt</strong></th>
    <th width="20%"><strong>Tiêu đề Tiếng Anh</strong></th>
    <th width="10%"><strong>Chuyên mục</strong></th>
    <th width="10%"><strong>Người tạo</strong></th>
    <th width="10%"><strong>Ngày tạo</strong></th>
    <th width="10%"><strong>Published</strong></th>
    <th width="15%"><strong>Thực hiện</strong></th>
  </tr>
  <?php	
   	$i=1;
	while($row=mysql_fetch_object($rs))
	{
			
  ?>
  <tr style="background:#e7e7e7;">
    <td align="center"><?php //echo $row->id;?><?php echo $i++;?></td>
    <td><?php echo $row->vi_title ?></td>
    <td><?php echo $row->en_title ?></td>
    <td><?php echo $row->sname?></td>
    <td><?php echo $row->username?></td>
    <td><?php echo $row->created_date?></td>
    <td><?php echo ($row->published==1)?"published":"unpublished"?></td>
   
    <td>
    	<a href="editarticle?aid=<?php echo $row->id?>">Edit/Detail</a>&nbsp;|&nbsp;<a href="" onclick="delArticle(<?php echo $row->id?>);return false;">Delete</a></td>
  </tr>
  <?php

   }
  ?>
  
  </table>
  <table cellpadding="3" width="740" border="0" align="left">
  <tr>
   <?php 
	echo ("<td>$PageLinks</td>"); 
   ?>
  </tr>
  </table>
</div>
